package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class WorkspaceServiceImpl implements WorkspaceService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {
        Map map = new HashMap();
        map.put("begin",begin);
        map.put("end",end);

        //总订单数 = 当日已完成订单的总金额
        Integer totalOrderCount = orderMapper.getOrderCount(map);

        map.put("status", Orders.COMPLETED);

        //有效订单数 = 当日已完成订单的数量
        Integer validOrderCount = orderMapper.getOrderCount(map);

        //订单完成率 = 有效订单数 / 总订单数
        Double orderCompletionRate = (double) validOrderCount/totalOrderCount;

        // 营业额：当前时段的number*amount
        Double turnover = orderMapper.selectTurnoverByDates(map);
        turnover = turnover == null? 0.0 : turnover;

        //平均客单价 = 营业额/有效订单数
        Double unitPrice = turnover / validOrderCount;

        //新增用户 = 当日新增用户的数量
        Integer newUsers = userMapper.getUserCount(map);

        return BusinessDataVO.builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(newUsers)
                .build();
    }

    public OrderOverViewVO getOverviewOrders(LocalDateTime begin, LocalDateTime end) {
        Map map = new HashMap();
        map.put("begin",begin);
        map.put("end",end);
        // 全部订单
        Integer allOrders = orderMapper.getOrderCount(map);

        //待接单数量
        map.put("status",Orders.TO_BE_CONFIRMED);
        Integer waitingOrders = orderMapper.getOrderCount(map);

        //待派送数量
        map.put("status",Orders.CONFIRMED);
        Integer deliveredOrders = orderMapper.getOrderCount(map);

        //已完成数量
        map.put("status",Orders.COMPLETED);
        Integer completedOrders = orderMapper.getOrderCount(map);

        //已取消数量
        map.put("status",Orders.CANCELLED);
        Integer cancelledOrders = orderMapper.getOrderCount(map);

        return OrderOverViewVO.builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders)
                .build();
    }

    @Override
    public DishOverViewVO getOverviewDishes() {
        Map map = new HashMap();
        map.put("status",1);
        // 已启售数量
        Integer sold = dishMapper.getByStatus(map);

        // 已停售数量
        map.put("status",0);
        Integer discontinued = dishMapper.getByStatus(map);

        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    public SetmealOverViewVO getOverviewSetmeals() {
        Map map = new HashMap();
        map.put("status",1);
        // 已启售数量
        Integer sold = setmealMapper.getByStatus(map);

        // 已停售数量
        map.put("status",0);
        Integer discontinued = setmealMapper.getByStatus(map);

        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }
}
